<?php
/**
 * 验收记录控制器
 */

namespace app\school\controller;

use app\common\model\Check;
use app\common\model\ComputeDetail;
use app\common\model\Dose;
use app\common\validate\CheckValidate;
use app\school\exception\ApiServiceException;
use app\school\service\CheckService;
use hg\apidoc\annotation as Apidoc;
use think\response\Json;

/**
 * @Apidoc\Title("验收记录")
 * @Apidoc\Group("index")
 */

class CheckController extends ApiBaseController {
	/**
	 * @Apidoc\Title("验收记录")
	 * @Apidoc\Desc("列表")
	 * @Apidoc\Method("GET")
	 * @Apidoc\Url("/school/check/index")
	 * @Apidoc\Returned("data",        type="int",                 desc="desc")
	 */
	public function index(CheckService $service): Json {
		try {
			$this->param['school_id'] = $this->uid;
			$data = $service->getList($this->param, $this->page, $this->limit);
			$result = [
				'check' => $data,
			];

			return api_success($result);
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * @Apidoc\Title("验收")
	 * @Apidoc\Desc("验收")
	 * @Apidoc\Method("POST")
	 * @Apidoc\Url("/school/check/add")
	 * @Apidoc\Param("id",          type="string",require=true, desc="未验收订单id")
	 * @Apidoc\Param("detail",          type="array",desc="提交内容",children={
	 *       	@Apidoc\Param("type_id", type="string", desc="商品"),
	 *        	@Apidoc\Param("spec_id", type="string", desc="规格"),
	 *        	@Apidoc\Param("brand_id", type="string", desc="单位"),
	 *        	@Apidoc\Param("num", type="string", desc="数量"),
	 *        	@Apidoc\Param("real_num", type="string", desc="实际数量"),
	 *    })
	 * @Apidoc\Returned("data",        type="int",                 desc="desc")
	 */
	public function add(CheckValidate $validate, CheckService $service): Json {
		$this->param['school_id'] = $this->uid;

		$compute_detail = ComputeDetail::where('id', $this->id)->find();
		$dose = Dose::where('id', $compute_detail['dose_id'])->find();

		$check = Check::where('user_id', $compute_detail['user_id'])->where('school_id', $compute_detail['school_id'])->where('order_id', $compute_detail['order_id'])->where('send_time', strtotime($dose['week']))->findOrEmpty();
		$this->param['send_time'] = $dose['week'];
		$this->param['order_id'] = $compute_detail['order_id'];
		$this->param['user_id'] = $compute_detail['user_id'];
		$this->param['detail'] = json_decode($this->param['detail'], true);
		$this->param['status'] = 1;
		if ($check->isEmpty()) {

			foreach ($this->param['detail'] as $key => $value) {
				if ($value['num'] != $value['real_num']) {
					$this->param['status'] = 2;
				}
			}
			unset($this->param['id']);
			$result = Check::create($this->param);
			ComputeDetail::where('id', $compute_detail['id'])->update(['status' => 1]);
		} else {
			$this->param['detail'] = array_merge($this->param['detail'], $check['detail']);
			foreach ($this->param['detail'] as $key => $value) {
				if ($value['num'] != $value['real_num']) {
					$this->param['status'] = 2;
				}
			}
			unset($this->param['id']);
			$result = $check->save($this->param);
			ComputeDetail::where('id', $compute_detail['id'])->update(['status' => 1]);
		}

		return $result ? api_success() : api_error();
	}

	/**
	 * 详情
	 * @param CheckValidate $validate
	 * @param CheckService $service
	 * @return Json
	 */
	public function info(CheckValidate $validate, CheckService $service): Json {
		$check = $validate->scene('api_info')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {

			$result = $service->getDataInfo($this->id);
			return api_success([
				'check' => $result,
			]);

		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 修改
	 * @param CheckService $service
	 * @param CheckValidate $validate
	 * @return Json
	 */
	public function edit(CheckService $service, CheckValidate $validate): Json {
		$check = $validate->scene('api_edit')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {
			$service->updateData($this->id, $this->param);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 删除
	 * @param CheckService $service
	 * @param CheckValidate $validate
	 * @return Json
	 */
	public function del(CheckService $service, CheckValidate $validate): Json {
		$check = $validate->scene('api_del')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {
			$service->deleteData($this->id);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

}
